package org.pro.Model;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class Shot extends Element {
    public double dir;
    public double speed = 220.0;
    public double damage = 20.0;
    public Tank tank;
    // 将非序列化字段标记为 transient
    transient public DrawInterface drawInterface;
    transient public JPanel panel;
    public static final long serialVersionUID = 5L;
    public Shot(Tank tank, double speed) {
        this.tank = tank;
        this.x = tank.x;
        this.y = tank.y;
        this.dir = tank.turretDir;
        this.speed = speed;
        this.panel=panel;
        this.tank.shotscount++;
    }
    //想要更改子弹啊嗷嗷！！
    public void update(double timeFlaps) {
        double len = this.speed * timeFlaps;
        this.move(this.dir, len);
        //更改游戏窗口大小
        if (this.x <0 || this.x > 860 || this.y <0|| this.y > 640) {
            this.destroy();
        }

    }

    public void draw(Graphics2D g, Object panel, CopyOnWriteArrayList<Gross> grossList) {
        if (!isInGross(grossList)) { // 仅在非草丛区域绘制
            this.drawInterface = (DrawInterface)panel;
            this.drawInterface.draw(this, g);
        }
    }

    public interface DrawInterface {
        void draw(Shot var1, Graphics2D var2);
    }
    //重写父类方法,如果被破坏了就进行删除呗
    public void destroy() {
        this.tank.shotscount--;
        this.Destroyed = true;
    }
    //判断是否在草丛中
    public boolean isInGross(CopyOnWriteArrayList<Gross> grossList) {
        Rectangle shotRect = this.getRect();
        for (Gross gross : grossList) {
            if (shotRect.intersects(gross.getRect())) {
                return true;
            }
        }
        return false;
    }
}

